package com.zrk.base;

/**
 * 可变参数练习
 * @author zrk
 *
 */
public class VariadicParameters {

    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        int c = 3;

        // 参数可以分别传入
        VariadicParameters variadicParameters = new VariadicParameters();
        int sum = variadicParameters.sum(a, b, c);
        System.out.println(sum);

        // 也可以使用数组
        int arr[] = new int[4];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = 2 * (i + 1);
        }
        int sum1 = variadicParameters.sum(arr);
        int sum2 = variadicParameters.sumB(5, arr);
        System.out.println(sum1);
        System.out.println(sum2);
    }

    /**
     * 可变参数求和
     * @param nums
     * @return
     */
    int sum(int... nums) {
        int ret = 0;
        for (int i = 0; i < nums.length; i++) {
            ret += nums[i];
        }

        return ret;
    }

    int sumB(int a, int ...nums){
        int sum = 0;
        sum += a;
        for (int i = 0; i < nums.length; i++){
            sum += nums[i];
        }
        return sum;
    }
}
